#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:HeWenjun
# datetime:2024/1/31 17:24
# software: PyCharm
import json
from rename_file import rename_and_copy_file


def read_json(file_path):
    """
    读取json文件并返回字典格式数据
    :param file_path: json文件路径
    :return:返回json内的所有数据dict
    """
    with open(file_path, 'r', encoding='utf-8') as info_json:
        info_dict = json.load(info_json)
    return info_dict


class Runner:
    def __init__(self):
        """
        完成读取json并提取参数
        """
        project_info = read_json('../data_json/system_info.json')
        test_reviewer = read_json('../data_json/test_reviewer.json')
        website = read_json('../data_json/url.json')
        self.full_info = {
            # 以下字段从system_info文件中读取
            'system': project_info['system'],
            'version': project_info['version'],
            'requirement_num': project_info['requirement_num'],
            'requirement_description': project_info['requirement_description'],
            'date': project_info['date'],
            # 以下字段，以system_info的system为键名，从test_reviewer文件中读取
            'manager': test_reviewer[project_info['system']],
            'developer': test_reviewer[project_info['system']],
            # 以下字段，以system_info的system为键名，从website文件中读取
            'url': website[project_info['system']]}

    def word(self, file_path):
        """
        修改word文档的模板
        :param file_path: 文档路径
        :return: None
        """
        import edit_word
        full_info = self.full_info
        for key, value in full_info.items():
            keyword = '{' + key + "}"
            # 修改用例评审确认书
            edit_word.replace_keyword(file_path=file_path, keyword=keyword, replacement=value)

    def excel(self, file_path):
        """
        修改excel文档的模板
        :param file_path:文档路径
        :return: None
        """
        import edit_excel
        full_info = self.full_info
        for key, value in full_info.items():
            keyword = '{' + key + "}"
            # 修改用例评审确认书
            edit_excel.replace_keyword(file_path=file_path, keyword=keyword, replacement=value)

    def main_step(self):
        # 将模板文件复制到输出文件夹，并完成命名，并获取paths列表
        file_paths = rename_and_copy_file(old_dir_path='../sample_file', new_dir_path='../output',
                                          system=self.full_info['system'], version=self.full_info['version'])[1]
        # 遍历文件路径，通过path针对不同附件执行修改
        for path in file_paths:
            if '功能' in path:
                self.word(file_path=path)
                print(path + '已修改---------------------------')
            elif '性能' in path:
                self.excel(file_path=path)
                print(path + '已修改---------------------------')
                pass
            elif '评审' in path:
                self.word(file_path=path)
                print(path + '已修改---------------------------')


if __name__ == '__main__':
    Runner().main_step()
